<?php
require_once '../Common/values.php';
require_once '../Common/database.php';

$recipient='svb@vospers.com';
$body='<center>EOD batch script '.$date.' @ '.$time.'</center>';
$con = mysql_connect("localhost",$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$body .= '<table>';
mysql_select_db($database, $con);
$body .= '<td><tr>Connected to database</td><td>OK</td></tr>';
mysql_query("UPDATE tblSettings SET lock='true' lockreason='EOD process' WHERE ID='0'");
$body .= '<td><tr>Locked system</td><td>OK</td></tr>';
mysql_query("UPDATE tbljobs SET Status='1st Line' WHERE Status='1st Line - In Progress'");
$body .='<td><tr>Updated  '.mysql_affected_rows().'</td><td>From 1st Line - In progress to 1st Line<td></td></tr>';
mysql_query("SELECT * FROM tbljobs WHERE Status=''");
if(mysql_affected_rows()>0)
{
$body .= '<tr><td><font color="red">Error Found '.mysql_affected_rows().' blank status!</font></td></tr>';
}
else
{
$body .= '<tr><td>Blank status check</td><td>OK</td></tr>';
}

mysql_query("SELECT * FROM tbljobs WHERE Project=''");
if(mysql_affected_rows()>0)
{
$body .= '<tr><td><font color="red">Error Found '.mysql_affected_rows().' blank projects!</font></td></tr>';
}
else
{
$body .= '<tr><td>Blank project check</td><td>OK</td></tr>';
}

mysql_query("SELECT * FROM tbljobs WHERE Closed='checked' AND Assigned='Not Assigned'");
if(mysql_affected_rows()>0)
{
$body .= '<tr><td><font color="red">Error Found '.mysql_affected_rows().' Closed not assigned jobs!</font></td></tr>';
}
else
{
$body .= '<tr><td>closed not assigned check</td><td>OK</td></tr>';
}
mysql_query("SELECT * FROM tblsolutions WHERE Operator=''");
if(mysql_affected_rows()>0)
{
$body .= '<tr><td><font color="red">Error Found '.mysql_affected_rows().' blank operator in tblsolutions</font></td></tr>';
}
else
{
$body .= '<tr><td>closed not assigned check</td><td>OK</td></tr>';
}
mysql_query("SELECT * FROM tblinventory WHERE Status='unknown'");
if(mysql_affected_rows()>0)
{
$body .= '<tr><td><font color="red">Error Found '.mysql_affected_rows().' Unknown inventory status!</font></td></tr>';
}
else
{
$body .= '<tr><td>Unknown inventory status check</td><td>OK</td></tr>';
}



require_once 'archive.php';


$query=mysql_query("CHECK TABLE `tblapplications` , `tblbranches` , `tbldashboard2` , `tbldepartment` , `tbldocuments` , `tblevents` , `tblfolders` , `tblfolders2` , `tblhstats` , `tblinventory` , `tblinventorystatus` , `tbljobs` , `tblkb` , `tblkbdocuments` , `tblkbfolders` , `tblkblog` , `tblldap` , `tbllinks` , `tblloan` , `tblpermisions` , `tblproirty` , `tblprojects` , `tblreports` , `tblselect` , `tblsettings` , `tblsolutions` , `tblstatus` , `tbltheme` , `tblusers` ");

while($row = mysql_fetch_array($query))
{
$body .= '<tr><td>Check '.$row[Table].'</td><td>'.$row[Msg_text].'</td></tr>';
}



$query=mysql_query("OPTIMIZE TABLE `tblapplications` , `tblbranches` , `tbldashboard2` , `tbldepartment` , `tbldocuments` , `tblevents` , `tblfolders` , `tblfolders2` , `tblhstats` , `tblinventory` , `tblinventorystatus` , `tbljobs` , `tblkb` , `tblkbdocuments` , `tblkbfolders` , `tblkblog` , `tblldap` , `tbllinks` , `tblloan` , `tblpermisions` , `tblproirty` , `tblprojects` , `tblreports` , `tblselect` , `tblsettings` , `tblsolutions` , `tblstatus` , `tbltheme` , `tblusers`");

while($row = mysql_fetch_array($query))
{
$body .= '<tr><td>Optimize '.$row[Table].'</td><td>'.$row[Msg_text].'</td></tr>';
}


$query=mysql_query("REPAIR TABLE `tblapplications` , `tblbranches` , `tbldashboard2` , `tbldepartment` , `tbldocuments` , `tblevents` , `tblfolders` , `tblfolders2` , `tblhstats` , `tblinventory` , `tblinventorystatus` , `tbljobs` , `tblkb` , `tblkbdocuments` , `tblkbfolders` , `tblkblog` , `tblldap` , `tbllinks` , `tblloan` , `tblpermisions` , `tblproirty` , `tblprojects` , `tblreports` , `tblselect` , `tblsettings` , `tblsolutions` , `tblstatus` , `tbltheme` , `tblusers`");

while($row = mysql_fetch_array($query))
{
$body .= '<tr><td> Repair '.$row[Table].'</td><td>'.$row[Msg_text].'</td></tr>';
}


$query=mysql_query("ANALYZE TABLE `tblapplications` , `tblbranches` , `tbldashboard2` , `tbldepartment` , `tbldocuments` , `tblevents` , `tblfolders` , `tblfolders2` , `tblhstats` , `tblinventory` , `tblinventorystatus` , `tbljobs` , `tblkb` , `tblkbdocuments` , `tblkbfolders` , `tblkblog` , `tblldap` , `tbllinks` , `tblloan` , `tblpermisions` , `tblproirty` , `tblprojects` , `tblreports` , `tblselect` , `tblsettings` , `tblsolutions` , `tblstatus` , `tbltheme` , `tblusers`");
while($row = mysql_fetch_array($query))
{
$body .= '<tr><td> Analyze '.$row[Table].'</td><td>'.$row[Msg_text].'</td></tr>';
}




mysql_query("UPDATE tblSettings SET lock='false', lockreason='' WHERE ID='0'");

$body .= '</table>';

ini_set('SMTP', $smtp);
ini_set('sendmail_from', 'vhs@vospers.com');
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0;' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1;' . "\n";
// Additional headers
$headers .= 'To: ;' . "\n";
$headers .= 'From:Helpdesk System <vhs@vospers.com>;' . "";
$Name = "Helpdesk System"; //senders name
$email = "VHS@vospers.com"; //senders e-mail adress
$subject='VHS EOD log';
mail($recipient, $subject, $body, $headers);

echo 'EOD Emailed <a href="../index.php">Back</a>';
?>